﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BartonExtension;
using Model;

namespace Backend
{
    public partial class FrmMall : DevExpress.XtraEditors.XtraForm
    {
        public DialogResult result { get; set; }
        public Mall mall { get; set; }
        public FrmMall()
        {
            InitializeComponent();
        }

        public FrmMall(Mall m)
        {
            InitializeComponent();
            mall = m;
            cmbRegion.Text = mall.region;
            txtCode.Text = mall.code.ToUpper();
            txtLat.Text = mall.latitude.ToString();
            txtLong.Text = mall.longitude.ToString();
            txtStreet.Text = mall.street;
            btnMall.EditValue = mall.name;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(cmbRegion.EditValue == null)
            {
                XtraMessageBox.Show("Empty region is not allowed");
                return;
            }

            if(btnMall.EditValue == null)
            {
                XtraMessageBox.Show("Empty mall name is not allowed");
                return;
            }

            if(txtLat.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Please fill up required data fields");
                return;
            }
            if(txtCode.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Please fill up required data fields");
                return;
            }
            this.mall = new Mall()
            {
                ID = -1,
                name = btnMall.EditValue.ToString(),
                region = cmbRegion.EditValue.ToString(),
                street = txtStreet.Text,
                code = txtCode.Text,
                latitude = txtLat.Text.toFloat().Value,
                longitude = txtLong.Text.toFloat().Value
            };
            result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
        
        private void btnMall_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if(btnMall.EditValue != null)
            {
                Utils.search(btnMall.EditValue.ToString(), "json", 0, 20, 1, "香港", (data, err) =>
                    {
                        this.Invoke(new Action(() =>
                            {
                                using (LocationPicker frm = new LocationPicker(data))
                                {
                                    frm.ShowDialog();
                                    if (frm.result == System.Windows.Forms.DialogResult.OK)
                                    {
                                        this.btnMall.EditValue = frm.chosen.name;
                                        this.txtStreet.Text = frm.chosen.street;
                                        this.txtLat.Text = frm.chosen.latitude.ToString();
                                        this.txtLong.Text = frm.chosen.longitude.ToString();
                                    }
                                }
                            }));
                    });
            }
        }
    }
}